<?php namespace Common\Model;
use Hdphp\Model\Model;
//商品模型
class Goods extends  Model{
	//指定表名
	protected $table="goods";
	private $GoodsdetailModel;
	private $GoodsattrModel;
	//定义构造函数
	public function __init(){
		$this->GoodsdetailModel=new \Common\Model\Goodsdetail;
		$this->GoodsattrModel=new \Common\Model\Goodsattr;
	}
	//自动验证
	protected $validate=array(
		//名字，验证，什么时候验证
		array('gname','required','商品名不为空',3,3),
		array('gunit','required','商品单位必填',3,3),
		array('gunit','maxlen:1','商品单位过长',3,3),
		array('gmprice','required','市场价格必填',3,3),
		array('gprice','required','商城价格必填',3,3),
		array('glistimg','required','列表图必填',3,3),

	);
	//自动添加
	protected $auto=array(
			array('gnum','gnum','method',3,3),
			array('gtime','time','function',3,3),
			array('admin_login_aid','admin_login_aid','method',3,3),
	);
	public function gnum(){
		$gnum = substr(date("ymdHis"),2,8).mt_rand(100000,999999);
		return $gnum;
	}
	public function store(){
		//商品表验证
		if(!$this->create()) return false;
		$gid=$this->add();
		
		//商品详情表验证 获得详情表里错误信息，添加到当前的错误信息
		if(!$this->GoodsdetailModel->create()) {
			$this->error=$this->GoodsdetailModel->getError();
			return false;
		}
		//添加数据到商品表，获得受影响的id
		
		//把商品gid添加到详情表中的超全局数组中
		$this->GoodsdetailModel->data['goods_gid']=$gid;
		//添加商品详情表数据
		$this->GoodsdetailModel->add();
		//组合数组接收post里面的attr数组
		$attr=Q('post.attr');
		foreach ($attr as $k=>$v){
			$attrs=array(
				'gavalue'=>$v,
				't_attr_taid'=>$k,
				'goods_gid'=>$gid,
			);
			//添加商品属性表
			$this->GoodsattrModel->add($attrs);
		}
		//组合post里面的spec数组
		$spec=$_POST['spec'];
//		p($_POST);die;
//		p($spec);
		foreach ($spec as $key=>$value){
			foreach ($value['value'] as $k=>$v){
				$specs=array(
					'gavalue'=>$v,
					't_attr_taid'=>$key,
					'goods_gid'=>$gid,
					'addprice'=>$value['price'][$k],
				);
				
				$this->GoodsattrModel->add($specs);
			}

		}
		return true;
	}
	public function admin_login_aid(){
		return $_SESSION['aid'];
		
	}
	public function edit(){
		//商品表验证
		if(!$this->create()) return false;
		//添加数据到商品表，获得受影响的id
		$gid=$this->save();
		//商品详情表验证 获得详情表里错误信息，添加到当前的错误信息
		if(!$this->GoodsdetailModel->create()) {
			$this->error=$this->GoodsdetailModel->getError();
			return false;
		}
		//把商品gid添加到详情表中的超全局数组中
		$this->GoodsdetailModel->data['goods_gid']=$gid;
		//添加商品详情表数据
		$this->GoodsdetailModel->save();
		$gid=Q('post.gid');
		p($gid);
		$W=$this->GoodsattrModel->where("goods_gid={$gid}")->delete();
	
		$attr=Q('post.attr');
		foreach ($attr as $k=>$v){
			$attrs=array(
				'gavalue'=>$v,
				't_attr_taid'=>$k,
				'goods_gid'=>$gid,
			);
			//添加商品属性表
			$this->GoodsattrModel->add($attrs);
		}
		//组合post里面的spec数组
		$spec=Q('post.spec',array());
//		p($_POST);die;
		foreach ($spec as $key=>$value){
			foreach ($value['value'] as $k=>$v){
				$specs=array(
					'gavalue'=>$v,
					't_attr_taid'=>$key,
					'goods_gid'=>$gid,
					'addprice'=>$value['price'][$k],
				);
				
				$this->GoodsattrModel->add($specs);
			}
		}
		return true;
		
	}


}




 ?>